/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.gft.bancosimulador.views;

import javax.swing.JOptionPane;
import com.gft.bancosimulador.beans.Agencia;
import com.gft.bancosimulador.business.AgenciaBusiness;

/**
 *
 * @author AEPI
 */
public class AgenciaView extends javax.swing.JInternalFrame {

    /**
     * Creates new form AgenciaView
     */
    public AgenciaView() {
        initComponents();
        
        
    }
    
    

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lbl_CodAgencia = new javax.swing.JLabel();
        txt_CodAgencia = new javax.swing.JTextField();
        btn_Consultar = new javax.swing.JButton();
        lbl_NomeAgencia = new javax.swing.JLabel();
        txt_NomeAgencia = new javax.swing.JTextField();
        lbl_CnpjAgencia = new javax.swing.JLabel();
        txt_CnpjAgencia = new javax.swing.JTextField();
        lbl_EnderecoAgencia = new javax.swing.JLabel();
        txt_EnderecoAgencia = new javax.swing.JTextField();
        ckb_AgenciaAtiva = new javax.swing.JCheckBox();
        btn_Salvar = new javax.swing.JButton();
        btn_Desativar = new javax.swing.JButton();
        btn_Reativar = new javax.swing.JButton();
        btn_Limpar = new javax.swing.JButton();

        setClosable(true);
        setIconifiable(true);
        setTitle("AGENCIA");
        setToolTipText("");

        lbl_CodAgencia.setText("Codigo Agencia:");

        btn_Consultar.setText("Consultar");
        btn_Consultar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_ConsultarActionPerformed(evt);
            }
        });

        lbl_NomeAgencia.setText("Nome:");

        lbl_CnpjAgencia.setText("CNPJ:");

        lbl_EnderecoAgencia.setText("Endereco:");

        ckb_AgenciaAtiva.setText("Agencia Ativa");

        btn_Salvar.setText("Salvar");
        btn_Salvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_SalvarActionPerformed(evt);
            }
        });

        btn_Desativar.setText("Desativar");
        btn_Desativar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_DesativarActionPerformed(evt);
            }
        });

        btn_Reativar.setText("Reativar");
        btn_Reativar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_ReativarActionPerformed(evt);
            }
        });

        btn_Limpar.setText("Limpar");
        btn_Limpar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_LimparActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(31, 31, 31)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(lbl_EnderecoAgencia)
                            .addComponent(lbl_CnpjAgencia)
                            .addComponent(lbl_NomeAgencia)
                            .addComponent(lbl_CodAgencia))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(txt_CodAgencia, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(26, 26, 26)
                                .addComponent(btn_Consultar, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(txt_NomeAgencia)
                            .addComponent(txt_EnderecoAgencia, javax.swing.GroupLayout.DEFAULT_SIZE, 330, Short.MAX_VALUE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(txt_CnpjAgencia, javax.swing.GroupLayout.PREFERRED_SIZE, 203, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(ckb_AgenciaAtiva))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(29, 29, 29)
                        .addComponent(btn_Salvar, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btn_Desativar, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btn_Reativar, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btn_Limpar, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(27, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(39, 39, 39)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbl_CodAgencia)
                    .addComponent(txt_CodAgencia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btn_Consultar))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbl_NomeAgencia)
                    .addComponent(txt_NomeAgencia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbl_CnpjAgencia)
                    .addComponent(txt_CnpjAgencia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(ckb_AgenciaAtiva))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbl_EnderecoAgencia)
                    .addComponent(txt_EnderecoAgencia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 39, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btn_Salvar)
                    .addComponent(btn_Desativar)
                    .addComponent(btn_Reativar)
                    .addComponent(btn_Limpar))
                .addGap(29, 29, 29))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btn_DesativarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_DesativarActionPerformed
        // TODO add your handling code here:
        Agencia agencia = this.preencherBeanAgencia(new Agencia());
        AgenciaBusiness.getInstance().desativarAgencia(agencia);
        JOptionPane.showMessageDialog(this, "Agencia desativada com sucesso!");
        this.limparTela();
        
    }//GEN-LAST:event_btn_DesativarActionPerformed

    private void btn_SalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_SalvarActionPerformed
        // TODO add your handling code here:
        Agencia novo = this.preencherBeanAgencia(new Agencia()); //cria objeto novo do tipo Bean. Chama o metodo para prencher o bean e cria um novo bean  
        AgenciaBusiness.getInstance().cadastrarAgencia(novo); //cria instancia da camada business e chama o metodo para cadastrar agencia passando o objeto criado na linha acima
        JOptionPane.showMessageDialog(this, "Agencia salva com sucesso!"); //exibe mensagem que deu tudo certo
        this.limparTela(); //limpa tela
    }//GEN-LAST:event_btn_SalvarActionPerformed

    private void btn_ReativarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_ReativarActionPerformed
        // TODO add your handling code here:
        Agencia agencia = this.preencherBeanAgencia(new Agencia()); //cria objeto novo do tipo Bean. Chama o metodo para preencher a Bean
        AgenciaBusiness.getInstance().reativarAgencia(agencia);//cria instancia e chama metodo reativar agencia e passa o bean como parametro
        JOptionPane.showMessageDialog(this, "Agencia reativada com sucesso!");
        this.limparTela();
        
    }//GEN-LAST:event_btn_ReativarActionPerformed

    private void btn_ConsultarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_ConsultarActionPerformed
        // TODO add your handling code here:
        int codigo = Integer.parseInt(txt_CodAgencia.getText()); //cria variavel para receber o String convertido
        Agencia agencia = AgenciaBusiness.getInstance().consultarAgencia(codigo); //cria instancia e chama o metodo para consultar agencia passando a variavel anterior como parametro
        this.mostrarBeanAgencia(agencia); //chama o metodo mostrar agencia para exibir as informacoes
        
    }//GEN-LAST:event_btn_ConsultarActionPerformed

    private void btn_LimparActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_LimparActionPerformed
        // TODO add your handling code here:
        this.limparTela();
    }//GEN-LAST:event_btn_LimparActionPerformed
   private void limparTela(){ //seta todos os campos como em branco
        this.txt_CnpjAgencia.setText("");
        this.txt_CodAgencia.setText("");
        this.txt_EnderecoAgencia.setText("");
        this.txt_NomeAgencia.setText("");
        this.ckb_AgenciaAtiva.setSelected(false);
    }

    private Agencia preencherBeanAgencia(Agencia agencia){ //recebe como parametro o Bean
        if (txt_CodAgencia.getText() != null && txt_CodAgencia.getText().length() > 0){ //verifica se o que foi digitado eh diferente de nulo e com tamanho maior que 0
            int codigoAgencia = Integer.parseInt(txt_CodAgencia.getText()); //cria variavel codigoAgencia que ira receber o valor digitado em int e nao mais em String
            agencia.setCodigo(codigoAgencia); //chama o metodo setCodigo do Bean Agencia e envia o valor digitado.
        }
        agencia.setCnpj(txt_CnpjAgencia.getText()); //chama metodo do Bean Agencia para gravar dados digitados.
        agencia.setAtiva(ckb_AgenciaAtiva.isSelected()); //chama metodo do Bean para setar agencia como ativa.
        agencia.setEndereco(txt_EnderecoAgencia.getText());
        agencia.setNome(txt_NomeAgencia.getText());
        return agencia; //devolve o Bean
    }

    private void mostrarBeanAgencia(Agencia agencia){ //recebe o Bean
        this.limparTela(); //chama o metodo limpar tela para que o formulario fique em branco
        if (agencia == null) //verifica se o Bean esta nulo, se estiver nao retorna nada.
            return;
        this.txt_CnpjAgencia.setText(agencia.getCnpj()); //recebe informacao vinda da Bean e devolve no TextField correspondente
        this.txt_CodAgencia.setText(String.valueOf(agencia.getCodigo())); //alem de pegar a informacao, converte para string
        this.txt_EnderecoAgencia.setText(agencia.getEndereco()); 
        this.txt_NomeAgencia.setText(agencia.getNome());
        this.ckb_AgenciaAtiva.setSelected(agencia.isAtiva());
    }


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btn_Consultar;
    private javax.swing.JButton btn_Desativar;
    private javax.swing.JButton btn_Limpar;
    private javax.swing.JButton btn_Reativar;
    private javax.swing.JButton btn_Salvar;
    private javax.swing.JCheckBox ckb_AgenciaAtiva;
    private javax.swing.JLabel lbl_CnpjAgencia;
    private javax.swing.JLabel lbl_CodAgencia;
    private javax.swing.JLabel lbl_EnderecoAgencia;
    private javax.swing.JLabel lbl_NomeAgencia;
    private javax.swing.JTextField txt_CnpjAgencia;
    private javax.swing.JTextField txt_CodAgencia;
    private javax.swing.JTextField txt_EnderecoAgencia;
    private javax.swing.JTextField txt_NomeAgencia;
    // End of variables declaration//GEN-END:variables
}
